package com.ruoyi.basedocument.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ruoyi.basebill.mapper.BillUtilMapper;
import com.ruoyi.basedocument.domain.BomRawmaterial;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basedocument.mapper.BomSpecialmaterialMapper;
import com.ruoyi.basedocument.domain.BomSpecialmaterial;
import com.ruoyi.basedocument.service.IBomSpecialmaterialService;

/**
 * BOM特殊物料Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-12
 */
@Service
public class BomSpecialmaterialServiceImpl implements IBomSpecialmaterialService 
{
    @Autowired
    private BomSpecialmaterialMapper bomSpecialmaterialMapper;

    @Autowired
    private BillUtilMapper billUtilMapper;

    /**
     * 查询BOM特殊物料
     * 
     * @param id BOM特殊物料主键
     * @return BOM特殊物料
     */
    @Override
    public BomSpecialmaterial selectBomSpecialmaterialById(String id)
    {
        return bomSpecialmaterialMapper.selectBomSpecialmaterialById(id);
    }

    /**
     * 查询BOM特殊物料列表
     * 
     * @param bomSpecialmaterial BOM特殊物料
     * @return BOM特殊物料
     */
    @Override
    public List<BomSpecialmaterial> selectBomSpecialmaterialList(BomSpecialmaterial bomSpecialmaterial)
    {
        return bomSpecialmaterialMapper.selectBomSpecialmaterialList(bomSpecialmaterial);
    }

    /**
     * 查询BOM特殊物料列表
     *
     * @param bomSpecialmaterial BOM特殊物料
     * @return BOM特殊物料
     */
    @Override
    public List<BomSpecialmaterial> selectBomSpecialmaterialList2(BomSpecialmaterial bomSpecialmaterial)
    {
        return bomSpecialmaterialMapper.selectBomSpecialmaterialList2(bomSpecialmaterial);
    }

    /**
     * 新增BOM特殊物料
     * 
     * @param bomSpecialmaterial BOM特殊物料
     * @return 结果
     */
    @Override
    public int insertBomSpecialmaterial(BomSpecialmaterial bomSpecialmaterial)
    {
        String billNumber = createBillNumber(bomSpecialmaterial);
        bomSpecialmaterial.setMaterialnumber(billNumber);
        bomSpecialmaterial.setCreatetime(new Date());
        return bomSpecialmaterialMapper.insertBomSpecialmaterial(bomSpecialmaterial);
    }

    /**
     * 修改BOM特殊物料
     * 
     * @param bomSpecialmaterial BOM特殊物料
     * @return 结果
     */
    @Override
    public int updateBomSpecialmaterial(BomSpecialmaterial bomSpecialmaterial)
    {
        return bomSpecialmaterialMapper.updateBomSpecialmaterial(bomSpecialmaterial);
    }

    /**
     * 批量删除BOM特殊物料
     * 
     * @param ids 需要删除的BOM特殊物料主键
     * @return 结果
     */
    @Override
    public int deleteBomSpecialmaterialByIds(String[] ids)
    {
        return bomSpecialmaterialMapper.deleteBomSpecialmaterialByIds(ids);
    }

    /**
     * 批量查询物料
     *
     * @return 结果
     */
    @Override
    public List<BomSpecialmaterial> selectBomSpecialmaterialListByNumber(String[] numbers)
    {
        return bomSpecialmaterialMapper.selectBomSpecialmaterialListByNumber(numbers);
    }

    /**
     * 删除BOM特殊物料信息
     * 
     * @param id BOM特殊物料主键
     * @return 结果
     */
    @Override
    public int deleteBomSpecialmaterialById(String id)
    {
        return bomSpecialmaterialMapper.deleteBomSpecialmaterialById(id);
    }

    public synchronized String createBillNumber(BomSpecialmaterial bomSpecialmaterial) {
        StringBuffer number = new StringBuffer("TS" + bomSpecialmaterial.getGroup1number() + ".");
        String formerBillNumber = billUtilMapper.getMaterialNumber(number.toString());
        //最终的单据编号
        String commentNumber = "";
        //判断数据库中是否存在数据
        if (formerBillNumber != null && formerBillNumber.contains(number)) {
            //从单据编号中截取后6位
            String billNumberEnd = formerBillNumber.substring(formerBillNumber.length() - 5);
            int endNumber = Integer.parseInt(billNumberEnd);
            int tempNumber = 1000000 + endNumber + 1;
            //将首位的1去掉之后，拼接成流水号
            commentNumber = number.append(String.valueOf(tempNumber).substring(1)).toString();
        } else {
            commentNumber = number.append("000001").toString();
        }

        return commentNumber;
    }
}
